//
//  LyyTextView.m
//  LyyTextView
//
//  Created by ylu on 15-3-25.
//  Copyright (c) 2015年 lyy. All rights reserved.
//


#import "LyyTextView.h"

static CGFloat const labelTopOffset = 6.f;
static CGFloat const labelLeftOffset = 8.f;
// On retina iPhones and iPads, the label is offset by 0.5 points:
static CGFloat const labelTopOffsetRetina = 0.5f;

@interface LyyTextView ()

@property (nonatomic, assign) CGFloat topLabelOffset;
@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation LyyTextView

- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        _topLabelOffset = labelTopOffset;
        [self finishInitialization];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame withTopLabelOffset:(CGFloat)topLabelOffset{
    self = [super initWithFrame:frame];
    if (self) {
        self.topLabelOffset = topLabelOffset;
        
        [self finishInitialization];
    }
    return self;
}

- (void)finishInitialization {
    
//    监听textView的value改变
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textChanged:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:self];
    
    CGFloat placeHolderLabelLeftOffset = labelLeftOffset;
    
    CGFloat placeHolderLabelTopOffset = self.topLabelOffset;
    
    // On retina iPhones and iPads, the label is offset by 0.5 points.
    if ([[UIScreen mainScreen] scale] == 2.0) {
        placeHolderLabelTopOffset += labelTopOffsetRetina;
    }
    
    CGSize labelOffset = CGSizeMake(placeHolderLabelLeftOffset, placeHolderLabelTopOffset);
    CGRect labelFrame = [self placeholderLabelFrameWithOffset:labelOffset];
    [self.placeholderLabel setFrame:labelFrame];
}

- (UILabel *)placeholderLabel{
    if (!_placeholderLabel) {
        
        _placeholderLabel = [[UILabel alloc] init];
        self.placeholderLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.placeholderLabel.numberOfLines = 0;
        self.placeholderLabel.font = self.font;
        self.placeholderLabel.backgroundColor = [UIColor clearColor];
        self.placeholderLabel.text = self.placeholderText;
        self.placeholderLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:self.placeholderLabel];
    }
    return _placeholderLabel;
}
- (CGRect)placeholderLabelFrameWithOffset:(CGSize)labelOffset {
    return CGRectMake(labelOffset.width,
                      labelOffset.height,
                      self.bounds.size.width  - (2 * labelOffset.width),
                      self.bounds.size.height - (2 * labelOffset.height));
}


#pragma mark - Custom accessors

- (void)setPlaceholderText:(NSString *)string {
    
    _placeholderText = [string copy];
    self.placeholderLabel.text = string;
    [self.placeholderLabel sizeToFit];
    
}


#pragma mark - UITextView subclass methods

- (void)setFont:(UIFont *)font {
    [super setFont:font];
    self.placeholderLabel.font = self.font;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    [super setTextAlignment:textAlignment];
    
    self.placeholderLabel.textAlignment = textAlignment;
}

// Todo: override setAttributedText to capture changes
// in text alignment?


#pragma mark - UITextInput overrides

// Listen to dictation events to hide the placeholder as is appropriate.

// Hide when there's a dictation result placeholder
- (id)insertDictationResultPlaceholder {
    // Call super.
    id placeholder = [super insertDictationResultPlaceholder];
    
    // Use -[setHidden] here instead of setAlpha:
    // these events also trigger -[textChanged],
    // which has a different criteria by which it shows the label,
    // but we undeniably know we want this placeholder hidden.
    self.placeholderLabel.hidden = YES;
    return placeholder;
}

// Update visibility when dictation ends.
- (void)removeDictationResultPlaceholder:(id)placeholder willInsertResult:(BOOL)willInsertResult {
    // Call super.
    [super removeDictationResultPlaceholder:placeholder willInsertResult:willInsertResult];
    
    // Unset the hidden flag from insertDictationResultPlaceholder.
    self.placeholderLabel.hidden = NO;
    
    // Update our text label based on the entered text.
    [self updatePlaceholderLabelVisibility];
}


#pragma mark - Text change listeners

- (void)updatePlaceholderLabelVisibility {
    if ([self.text length] == 0) {
        self.placeholderLabel.alpha = 1.f;
    } else {
        self.placeholderLabel.alpha = 0.f;
    }
}

// When text is set or changed, update the label's visibility.

- (void)setText:(NSString *)text {
    // Call super.
    [super setText:text];
    
    [self updatePlaceholderLabelVisibility];
}

- (void)textChanged:(NSNotification *)notification {
    [self updatePlaceholderLabelVisibility];
}


@end
